#!/bin/sh
# Set up any defaults for UCI, run once post installation
# Karl Palsson, OpenWrt.org, 2012
THISAPP=mosquitto
DEPENDS_ON=""
# Optional, defaults to $THISAPP
#THISAPP_INIT=

#### Should not need any changes below here........

add_depends () {
    dep=$1
    echo "Adding dependency for ${THISAPP} on $dep"
    SEC=$(uci -q get ucitrack.@$dep[-1])
    if [ $? -eq 1 ]; then
        # ucitrack section didn't even exist!
        uci add ucitrack $dep
    fi
    EXISTING=$(uci -q -d';' get ucitrack.@$dep[-1].affects | grep ${THISAPP})
    if [ $? -eq 1 ]; then
        # list doesn't exist, or didn't contain us.
        uci add_list ucitrack.@$dep[-1].affects=${THISAPP}
    else
        echo "affects list for $dep already contains ${THISAPP}"
    fi
}

# First install our own init script links....
BLAH=$(uci -q get ucitrack.@${THISAPP}[-1])
if [ $? -eq 1 ]; then
    # section doesn't exist....
    uci add ucitrack ${THISAPP}
fi
INIT=$(uci -q get ucitrack.@${THISAPP}[-1].init)
if [ $? -eq 1 ]; then
    # section existed, but not the init entry..."
    uci set ucitrack.@${THISAPP}[-1].init=${THISAPP_INIT:-${THISAPP}}
else
    echo "${THISAPP} section in ucitrack already exists, skipping"
fi

for dep in $DEPENDS_ON; do
    add_depends $dep
done

uci commit ucitrack
